<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Session;

class Index extends Controller
{
    //首页 - 登陆页
    public function index()
    {
        if(Session::has('admin_user')){
            //return $this->redirect('admin/main/index');
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //验证码
            if (!captcha_check($data['verify'])) {
                return json_x(-1,'验证码输入错误！');
            }
            // 验证数据
            $rule = [
                'username|用户名' => 'require|alphaDash|length:3,20',
                'password|密码' => 'require|length:3,20',
            ];
            $result = $this->validate($data, $rule);
            if (true !== $result) {
                return json_x(-1,$result);
            }
            $Admin = new Admin;
            if($Admin->login($data['username'],$data['password'])){
                return json_x(0,'登陆成功');
            }else{
                return json_x(-1,'登陆失败');
            }
        } else {
            return $this->fetch('admin/index');
        }
    }
}
